package com.lalilu.lmedia.scanner

import com.lalilu.lmedia.entity.LSong
import com.lalilu.lmedia.extension.SourceType
import com.lalilu.lmedia.extension.mediaUri

data class Audio(
    var id: Long,
    var title: String,
    var album: String,
    var albumId: Long,
    var artist: String,
    var artistId: String,
    var albumArtist: String? = null,
    var data: String? = null,
    var dateAdded: Long? = null,
    var dateModified: Long? = null,
    var fileName: String? = null,
    var extensionMimeType: String,
    var formatMimeType: String? = null,
    var duration: Long,
    var size: Long,
    var track: Int? = null,
    var disc: Int? = null
) {
    companion object {
        const val INVALID_ARTIST = "<unknown>"
    }

    fun toSong(): LSong? {
        try {
            return LSong(
                // Assert that the fields that should always exist are present. I can't confirm
                // that every device provides these fields, but it seems likely that they do.
                id = id.toString(),
                name = title,
                uri = id.mediaUri(),
                _artist = artist,
                _albumId = albumId.toString(),
                _albumArtist = albumArtist,
                _albumTitle = album,
                fileName = fileName,
                pathStr = data,
                mimeType = extensionMimeType,
                durationMs = duration,
                size = size,
                track = track,
                disc = disc,
                dateAdded = dateAdded,
                dateModified = dateModified,
                sourceType = SourceType.MediaStore
            )
        } catch (e: Exception) {
            println(e.message)
            return null
        }
    }
}